<template>
  <!-- html -->
  <div class="person">
    <h1>情况三，监视【reactive】定义的【对象类型数据】</h1>
    <h2>姓名:{{ person.name }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <button @click="changeName">修改名字</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changePerson">换人</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { reactive, ref, watch } from "vue";
let person = reactive({
  name: "xiaoji",
  age: 18,
});
// 方法
function changeName() {
  person.name += "~";
}
function changeAge() {
  person.age += 1;
}
function changePerson() {
  Object.assign(person, { name: "李四", age: 90 }); //不是真正意义的替换，地址不变，值覆盖
}

//监视 情况三，监视【reactive】定义的【对象类型数据】 默认开启深度监视不可关闭
watch(
  person,
  (newValue, oldValue) => {
    //对象一直不变
    console.log("person变化了", newValue, oldValue);
  },
  { deep: true }
);
</script>

<style scoped>
/* 样式 */
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
button {
  margin: 0 5px;
}
</style>
